package com.yumeng.config.helper;

import com.yumeng.common.buss.area.*;
import com.yumeng.common.exception.YumengException;
import com.yumeng.config.properties.YumengBussProperties;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wxd
 * @date 2023/5/31 13:54
 */
@Slf4j
public class RuntimeHelper {

    public static CurrSysAreaInfo getCurrSysAreaInfo(YumengBussProperties.SysArea areaProp){
        if (AreaHelper.isInit()){
            throw new YumengException("AreaHelper已初始化");
        }
        CurrSysAreaInfo areaInfo = null;
        //优先使用项目yml配置
        if (areaProp != null && areaProp.hasUsefulAreaInfo()){
            areaInfo = new CurrSysAreaInfo(areaProp.getSysAreaLevel(), areaProp.getSysAreaCode(), areaProp.getSysAreaName(), areaProp.getAreaBussMode());
        }else{//然后使用自动配置
            AreaBussMode defaultMode = areaProp != null && areaProp.getAreaBussMode() != null ? areaProp.getAreaBussMode() : AreaBussMode.SINGLE_CODE;
            if (EnvHelper.isDefault()){
                areaInfo = new CurrSysAreaInfo(SysAreaLevel.TOP, AreaConstants.AREA_CODE_TOP, AreaConstants.AREA_NAME_TOP, defaultMode);
            } else if (EnvHelper.isJiangSu()) {
                areaInfo = new CurrSysAreaInfo(SysAreaLevel.PROVINCE, AreaConstants.AREA_CODE_PROVINCE_JS, AreaConstants.AREA_NAME_PROVINCE_JS, defaultMode);
            }else if (EnvHelper.isShan3Xi()) {
                areaInfo = new CurrSysAreaInfo(SysAreaLevel.PROVINCE, AreaConstants.AREA_CODE_PROVINCE_S3X, AreaConstants.AREA_NAME_PROVINCE_S3X, defaultMode);
            }else if (EnvHelper.isShanDong()) {
                areaInfo = new CurrSysAreaInfo(SysAreaLevel.PROVINCE, AreaConstants.AREA_CODE_PROVINCE_SD, AreaConstants.AREA_NAME_PROVINCE_SD, defaultMode);
            }else {
                throw new YumengException("暂未自动适配当前地区：%s", EnvHelper.getSysFlag());
            }
        }
        return areaInfo;
    }

}
